package com.web.controller.api;

import com.web.annotation.UserAccess;
import com.web.dao.AddressMapper;
import com.web.domain.ApiResponse;
import com.web.entity.Address;
import com.web.manager.GaoDeManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * @author FDjavaone
 * @date 2017/8/22
 */


@RestController
@RequestMapping("api")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;


    /**
     * 收货地址创建
     */
    @UserAccess
    @RequestMapping("/app/address/add")
    public ApiResponse add(Address address, HttpServletRequest req, HttpServletResponse resp) {
        ApiResponse response = new ApiResponse();
        Date date = new Date();
        address.setCreateTime(date);
        address.setModifyTime(date);
        addressMapper.insertSelective(address);
        List<Address> list = addressMapper.selectByMid(address.getmId());
        response.setParams(list);
        response.setMessage("收货地址新增成功！");
        response.setStatus(900000);
        return response;
    }

    /**
     * 收货地址删除
     */
    @UserAccess
    @RequestMapping("/app/address/delete")
    public ApiResponse delete(Address address, HttpServletRequest req, HttpServletResponse resp) {
        ApiResponse response = new ApiResponse();
        addressMapper.deleteByPrimaryKey(address.getId());
        List<Address> list = addressMapper.selectByMid(address.getmId());
        response.setParams(list);
        response.setMessage("收货地址删除成功！");
        response.setStatus(900000);
        return response;
    }

    /**
     * 收货地址修改
     */
    @UserAccess
    @RequestMapping("/app/address/edit")
    public ApiResponse edit(Address address, HttpServletRequest req, HttpServletResponse resp) {
        ApiResponse response = new ApiResponse();
        Date date = new Date();
        address.setModifyTime(date);
        addressMapper.updateByPrimaryKeySelective(address);
        List<Address> list = addressMapper.selectByMid(address.getmId());
        response.setParams(list);
        response.setMessage("收货地址修改成功！");
        response.setStatus(900000);
        return response;
    }

    /**
     * 切换默认收货地址
     */
    @UserAccess
    @RequestMapping("/app/address/addressSwitch")
    public ApiResponse addressSwitch(Address address, HttpServletRequest req, HttpServletResponse resp) {
        ApiResponse response = new ApiResponse();
        Date date = new Date();
        address.setModifyTime(date);
        address.setDefaultStatus(0);
        addressMapper.setAllAddressNotDefault(address);
        address.setDefaultStatus(1);
        addressMapper.updateByPrimaryKeySelective(address);
        List<Address> list = addressMapper.selectByMid(address.getmId());
        response.setParams(list);
        response.setMessage("默认收货地址设置成功！");
        response.setStatus(900000);
        return response;
    }

    /**
     * 收货地址列表获取
     */
    @UserAccess
    @RequestMapping("/app/address/addressList")
    public ApiResponse addressList(Address address, HttpServletRequest req, HttpServletResponse resp) {
        ApiResponse response = new ApiResponse();
        List<Address> list = addressMapper.selectByMid(address.getmId());
        response.setParams(list);
        response.setMessage("收货地址获取成功！");
        response.setStatus(900000);

        return response;
    }

    /**
     * 地址转loc
     */
    @RequestMapping("/app/address/transferAddress")
    public ApiResponse transferAddress(String address) {
        return GaoDeManager.transferAddress(address);
    }

    /**
     * loc转地址
     */
    @RequestMapping("/app/address/transferLoc")
    public ApiResponse transferLoc(String loc) {
        return GaoDeManager.transferLoc(loc);
    }

}
